// pages/myself/myspellgroup/MySpellGroup.js
// pages/product/groupList.js
var Zan = require('../../../dist/index.js');
let network = null;
let utils = null;
let api = null;
let app = getApp()
var start = 0;
function countdown(that) {
  for (var i = 0; i < that.data.groupList.length; i++) {
    var EndTime = that.data.groupList[i].endDate * 1000 || [];
    var NowTime = new Date().getTime();
    var total_micro_second = EndTime - NowTime || [];
    // 渲染倒计时时钟
    that.data.groupList[i].clock = dateformat(total_micro_second)
    // that.setData({
    //   clock: dateformat(total_micro_second)
    // });
    if (total_micro_second <= 0) {
      that.data.groupList[i].clock = "已经截止"
      //return;
    }
  }
  that.setData({
    groupList: that.data.groupList
  })
  
  setTimeout(function () {
    total_micro_second -= 1000;
    countdown(that);
  }, 1000)

}

// 时间格式化输出，如11:03 25:19 每1s都会调用一次
function dateformat(micro_second) {
  // 总秒数
  var second = Math.floor(micro_second / 1000);
  // 天数
  var day = Math.floor(second / 3600 / 24) < 10 ? '0' + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  // 小时
  var hr = Math.floor(second / 3600 % 24) < 10 ? '0' + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  // 分钟
  var min = Math.floor(second / 60 % 60) < 10 ? '0' + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  // 秒
  var sec = Math.floor(second % 60) < 10 ? '0' + Math.floor(second % 60) : Math.floor(second % 60);
  let date = '';
  if (day == 0) {
    date = hr + ":" + min + ":" + sec;
  } else {
    date = day + ":" + hr + ":" + min + ":" + sec;
  }
  return date;
}

Page(Object.assign({}, Zan.Tab, Zan.TopTips, {

  /**
   * 页面的初始数据
   */
  data: {
    groupList: {},
    storeId: null,
    nodata: {
      msg: '暂无拼团商品',
      notype: 1,
      imgurl: '../../../images/nodatas.png',
      nourl: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    this.setData({
      storeId: app.globalData.store.id
    })

    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    start = 0;
    this.setData({
      groupList:[]
    })
    network._post_page_json(this, api.mySpellGroups, start, { storeId: this.data.storeId}, success);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.groupList.length < this.data.recordsTotal) {
      start = start + 20;
      network._post_page_json(this, api.mySpellGroups, start, { storeId: this.data.storeId}, success);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  //   let price = 0;
  //   let pricet2 = 0;
  //   if (this.data.userSpellGroupId) {
  //     price = this.data.proprice;
  //     pricet2 = this.data.orderprice;
  //   } else {
  //     if (this.data.listspecid === '') {
  //       price = this.data.RealListspec.specValue1;
  //       pricet2 = this.data.product.sgPrice;
  //     } else {
  //       price = this.data.product.proPrice
  //       pricet2 = this.data.RealListspec.specValue2;
  //     }
  //   }
  //   return {
  //     title: '花' + price2 / 100 + '元，就能拼得原价' + price / 100 + '元的【' + this.data.product.title + '】，太超值了',
  //     path: '/pages/product/SpellGroupDetails?shared=' + app.globalData.userId + '&productId=' + this.data.product.id + '&spellGroupId=' + this.data.spellGroup.id,
  //     // imageUrl: this.data.saleInfo.qrCode,
  //     success: function (res) {
  //       // 转发成功
  //       if (res.shareTickets) {
  //         wx.getShareInfo({
  //           shareTicket: res.shareTickets,
  //           success: function (res) {
  //             network._post_json(this, api.updateFxcs, { directShareNums: 0, directShareQunNums: 1 }, fxcsSuccess);
  //           },
  //           fail: function (res) {
  //             // 转发失败
  //             network._post_json(this, api.updateFxcs, { directShareNums: 1, directShareQunNums: 0 }, fxcsSuccess);
  //           }
  //         })
  //       } else {
  //         network._post_json(this, api.updateFxcs, { directShareNums: 1, directShareQunNums: 0 }, fxcsSuccess);
  //       }

  //     },
  //     fail: function (res) {
  //       // 转发失败
  //     }
  //   }
  // },
  onProductItemClick: function (e) {
    var isGroups = e.currentTarget.dataset.kaituandata.isGroups;
    var spellGroupsId = e.currentTarget.dataset.kaituandata.id;
    var unigroupId = e.currentTarget.dataset.kaituandata.unigroupId;
    var shengyu = e.currentTarget.dataset.kaituandata.shengyu;
    // console.log(unigroupId);
    // if (e.currentTarget.dataset.kaituandata.spellGroupsDetail) {
    //   var userSpellGroupId = e.currentTarget.dataset.kaituandata.spellGroupsDetail.id;
    //   var endtime = e.currentTarget.dataset.kaituandata.spellGroupsDetail.endDate;
    //   var originator = e.currentTarget.dataset.kaituandata.spellGroupsDetail.originator;
    //   var order = e.currentTarget.dataset.kaituandata.spellGroupsDetail.orderProductDetail;
    //   var Cantuan_spec = {};
    //   var Cantuan_specs = [];
    //   if (order.productSpec && !order.productSpec1 && !order.productSpec2) {
    //     var Spec = e.currentTarget.dataset.kaituandata.specType1 + ':' + order.productSpec
    //   } else if (order.productSpec1 && !order.productSpec2) {
    //     var Spec = e.currentTarget.dataset.kaituandata.specType1 + ':' + order.productSpec + ',' + e.currentTarget.dataset.kaituandata.specType2 + ':' + order.productSpec1
    //   } else if (order.productSpec2) {
    //     var Spec = e.currentTarget.dataset.kaituandata.specType1 + ':' + order.productSpec + ',' + e.currentTarget.dataset.kaituandata.specType2 + ':' + order.productSpec1 + ',' + e.currentTarget.dataset.kaituandata.specType3 + ':' + order.productSpec2
    //   }
    //   var count = e.currentTarget.dataset.kaituandata.spellGroupsDetail.orderProductDetail.count;
    //   Cantuan_spec.Spec = Spec;
    //   Cantuan_spec.count = count
    //   Cantuan_specs.push(Cantuan_spec);
    //   try {
    //     wx.setStorageSync('Cantuan_spec', Cantuan_specs)
    //   } catch (e) {
    //   }
    // }
    // if (e.currentTarget.dataset.kaituandata.spellGroupsCombs) {
    //   var sgCombId = e.currentTarget.dataset.kaituandata.spellGroupsCombs[0].id;
    // }

    // wx.navigateTo({
    //   url: '/pages/myself/myspellgroup/MySpellGroupDetails?userSpellGroupId=' + userSpellGroupId + '&spellGroupsId=' + spellGroupsId + '&sgCombId=' + sgCombId + '&endtime=' + endtime + '&originator=' + originator,
    // })

    wx.navigateTo({
      url: '/pages/myself/myspellgroup/MySpellGroupDetails?unigroupId=' + unigroupId + '&shengyu=' + shengyu,
    })
  }
}))

var success = function (that, res) {
  if (res.data.data) {
    that.setData({
      groupList: res.data.data,
      recordsTotal:res.data.data.length
    })
    countdown(that);
    
  }

}

